# -*- coding: utf-8 -*-

from odoo import models, fields, api


class wliotPartner(models.Model):
    # _name = 'wliot.partner'
    _inherit = 'res.partner'

    gps_longitude = fields.Float(string=u'gps经度')
    gps_latitude = fields.Float(string=u'gps纬度')
    password = fields.Char(string=u'密码', default="123456", password=True)
    setting_permission = fields.Boolean(string=u'设置权限', default=False)
    control_permission = fields.Boolean(string=u'控制权限', default=False)
    note = fields.Text(string=u'备注')
    bind_status = fields.Boolean(string=u'绑定状态')
    head_icon = fields.Char(string=u'头像')
    child_account_ids = fields.One2many('res.partner', 'parent_id', string=u'子账号')
    province_id = fields.Many2one('mxbase.province', string=u'省/自治区/直辖市', required=True)
    city_id = fields.Many2one('mxbase.city', string=u'市', required=True)
    county_id = fields.Many2one('mxbase.county', string=u'区/县', required=True)
    partner_type = fields.Selection([
        ('customer', u'客户'),
        ('mainuser', u'维修人员')
    ], string=u'用户类型', default='customer', required=True)
    wrong_count = fields.Integer(string=u'密码验证错误次数', default=1)

    @api.multi
    def get_message(self):
        self.ensure_one()
        province = self.env['mxbase.province'].sudo().search([('id', '=', self.province_id.id)])
        city = self.env['mxbase.city'].sudo().search([('id', '=', self.city_id.id)])
        county = self.env['mxbase.county'].sudo().search([('id', '=', self.county_id.id)])
        return {
            'name': self.name,
            'mobile': self.phone,
            'password': self.password,
            'address': '%s%s%s%s' % (province.name, city.name, county.name, self.street),
            'setting_permission': self.setting_permission,
            'control_permission': self.control_permission,
        }

    @api.multi
    def get_child(self):
        self.ensure_one()
        partners_ids = []
        if self.partner_type == 'mainuser':
            partners = self.env['res.partner'].sudo().search([
                ('partner_type', '=', 'customer'),
                ('parent_id', '=', self.id)
            ])
            for pp in partners:
                partners_ids.append(pp.id)
            partners_ids.append(self.id)
            return partners_ids
        elif self.partner_type == 'customer':
            partners = self.env['res.partner'].sudo().search([
                ('partner_type', '=', 'customer'),
                ('parent_id', '=', self.parent_id.id)
            ])
            for pp in partners:
                partners_ids.append(pp.id)
            partners_ids.append(self.parent_id.id)
            return partners_ids
